/*
 * Copyright (c) 1999-2009 InfoReach, Inc. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of
 * InfoReach ("Confidential Information"). You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with InfoReach.
 *
 * CopyrightVersion 2.0
 *
 */
package com.inforeach.util.string;

/**
 * @author dmytros
 *
 */
public class SpecialStringTransformer implements IStringTransformer
{
    public static final char DEFAULT_SPECIAL = '_';
    private final char special_;
    private final String specialStr_;

    public SpecialStringTransformer()
    {
        this(DEFAULT_SPECIAL);
    }

    public SpecialStringTransformer(char special)
    {
        special_ = special;
        specialStr_ = String.valueOf(special);
    }

    boolean special(String str)
    {
        for (int i = 0, n = str.length(); i < n; ++i)
        {
            if (str.charAt(i) != special_)
            {
                return false;
            }
        }
        return true;
    }

    public String forward(String str)
    {
        if (str == null) return "";
        return special(str) ? specialStr_.concat(str) : str;
    }

    public String reverse(String str)
    {
        if (str.isEmpty()) return null;
        return special(str) ? str.substring(1) : str;
    }
}
